package com.weetop.akly.utils.widget;


import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.support.v4.view.animation.FastOutSlowInInterpolator;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.TextView;

import com.lljjcoder.Interface.OnCityItemClickListener;
import com.lljjcoder.bean.CityBean;
import com.lljjcoder.bean.DistrictBean;
import com.lljjcoder.bean.ProvinceBean;
import com.lljjcoder.citywheel.CityParseHelper;
import com.lljjcoder.style.cityjd.AreaAdapter;
import com.lljjcoder.style.cityjd.CityAdapter;
import com.lljjcoder.style.cityjd.ProvinceAdapter;
import com.lljjcoder.style.citylist.Toast.ToastUtils;
import com.lljjcoder.style.citypickerview.R.id;
import com.lljjcoder.style.citypickerview.R.style;
import com.lljjcoder.utils.utils;
import com.weetop.akly.R;

import java.util.List;

public class JDCityPicker {
    private ListView mCityListView;
    private TextView mProTv;
    private TextView mCityTv;
    private TextView mAreaTv;
    private ImageView mCloseImg;
    private PopupWindow popwindow;
    private View mSelectedLine;
    private View popview;
    private CityParseHelper parseHelper;
    private ProvinceAdapter mProvinceAdapter;
    private CityAdapter mCityAdapter;
    private AreaAdapter mAreaAdapter;
    private List<ProvinceBean> provinceList = null;
    private List<CityBean> cityList = null;
    private List<DistrictBean> areaList = null;
    private int tabIndex = 0;
    private Context context;
    private String colorSelected = "#ff181c20";
    private String colorAlert = "#ffff4444";
    private OnCityItemClickListener mBaseListener;
    private Handler mHandler = new Handler(new Callback() {
        public boolean handleMessage(Message msg) {
            switch(msg.what) {
                case -1:
                    JDCityPicker.this.provinceList = (List)msg.obj;
                    JDCityPicker.this.mProvinceAdapter.notifyDataSetChanged();
                    JDCityPicker.this.mCityListView.setAdapter(JDCityPicker.this.mProvinceAdapter);
                    break;
                case 0:
                    JDCityPicker.this.provinceList = (List)msg.obj;
                    JDCityPicker.this.mProvinceAdapter.notifyDataSetChanged();
                    JDCityPicker.this.mCityListView.setAdapter(JDCityPicker.this.mProvinceAdapter);
                    break;
                case 1:
                    JDCityPicker.this.cityList = (List)msg.obj;
                    JDCityPicker.this.mCityAdapter.notifyDataSetChanged();
                    if (JDCityPicker.this.cityList != null && !JDCityPicker.this.cityList.isEmpty()) {
                        JDCityPicker.this.mCityListView.setAdapter(JDCityPicker.this.mCityAdapter);
                        JDCityPicker.this.tabIndex = 1;
                    }
                    break;
                case 2:
                    JDCityPicker.this.areaList = (List)msg.obj;
                    JDCityPicker.this.mAreaAdapter.notifyDataSetChanged();
                    if (JDCityPicker.this.areaList != null && !JDCityPicker.this.areaList.isEmpty()) {
                        JDCityPicker.this.mCityListView.setAdapter(JDCityPicker.this.mAreaAdapter);
                        JDCityPicker.this.tabIndex = 2;
                    }
            }

            JDCityPicker.this.updateTabsStyle(JDCityPicker.this.tabIndex);
            JDCityPicker.this.updateIndicator();
            return true;
        }
    });

    public JDCityPicker() {
    }

    public void setOnCityItemClickListener(OnCityItemClickListener listener) {
        this.mBaseListener = listener;
    }

    private void initJDCityPickerPop() {
        this.tabIndex = 0;
        if (this.parseHelper == null) {
            this.parseHelper = new CityParseHelper();
        }

        if (this.parseHelper.getProvinceBeanArrayList().isEmpty()) {
            ToastUtils.showLongToast(this.context, "请在Activity中增加init操作");
        } else {
            LayoutInflater layoutInflater = LayoutInflater.from(this.context);
            this.popview = layoutInflater.inflate(R.layout.pop_jdcitypicker, (ViewGroup)null);
            this.mCityListView = (ListView)this.popview.findViewById(id.city_listview);
            this.mProTv = (TextView)this.popview.findViewById(id.province_tv);
            this.mCityTv = (TextView)this.popview.findViewById(id.city_tv);
            this.mAreaTv = (TextView)this.popview.findViewById(id.area_tv);
            this.mCloseImg = (ImageView)this.popview.findViewById(id.close_img);
            this.mSelectedLine = this.popview.findViewById(id.selected_line);
            this.popwindow = new PopupWindow(this.popview, -1, -2);
            this.popwindow.setAnimationStyle(style.AnimBottom);
            this.popwindow.setBackgroundDrawable(new ColorDrawable());
            this.popwindow.setTouchable(true);
            this.popwindow.setOutsideTouchable(false);
            this.popwindow.setFocusable(true);
            this.popwindow.setOnDismissListener(new OnDismissListener() {
                public void onDismiss() {
                    utils.setBackgroundAlpha(JDCityPicker.this.context, 1.0F);
                }
            });
            this.mCloseImg.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    JDCityPicker.this.hidePop();
                    utils.setBackgroundAlpha(JDCityPicker.this.context, 1.0F);
                    if (JDCityPicker.this.mBaseListener != null) {
                        JDCityPicker.this.mBaseListener.onCancel();
                    }

                }
            });
            this.mProTv.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    JDCityPicker.this.tabIndex = 0;
                    if (JDCityPicker.this.mProvinceAdapter != null) {
                        JDCityPicker.this.mCityListView.setAdapter(JDCityPicker.this.mProvinceAdapter);
                        if (JDCityPicker.this.mProvinceAdapter.getSelectedPosition() != -1) {
                            JDCityPicker.this.mCityListView.setSelection(JDCityPicker.this.mProvinceAdapter.getSelectedPosition());
                        }
                    }

                    JDCityPicker.this.updateTabVisible();
                    JDCityPicker.this.updateIndicator();
                }
            });
            this.mCityTv.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    JDCityPicker.this.tabIndex = 1;
                    if (JDCityPicker.this.mCityAdapter != null) {
                        JDCityPicker.this.mCityListView.setAdapter(JDCityPicker.this.mCityAdapter);
                        if (JDCityPicker.this.mCityAdapter.getSelectedPosition() != -1) {
                            JDCityPicker.this.mCityListView.setSelection(JDCityPicker.this.mCityAdapter.getSelectedPosition());
                        }
                    }

                    JDCityPicker.this.updateTabVisible();
                    JDCityPicker.this.updateIndicator();
                }
            });
            this.mAreaTv.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    JDCityPicker.this.tabIndex = 2;
                    if (JDCityPicker.this.mAreaAdapter != null) {
                        JDCityPicker.this.mCityListView.setAdapter(JDCityPicker.this.mAreaAdapter);
                        if (JDCityPicker.this.mAreaAdapter.getSelectedPosition() != -1) {
                            JDCityPicker.this.mCityListView.setSelection(JDCityPicker.this.mAreaAdapter.getSelectedPosition());
                        }
                    }

                    JDCityPicker.this.updateTabVisible();
                    JDCityPicker.this.updateIndicator();
                }
            });
            this.mCityListView.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    JDCityPicker.this.selectedList(position);
                }
            });
            utils.setBackgroundAlpha(this.context, 0.5F);
            this.updateIndicator();
            this.updateTabsStyle(-1);
            this.setProvinceListData();
        }
    }

    private void selectedList(int position) {
        switch(this.tabIndex) {
            case 0:
                ProvinceBean provinceBean = this.mProvinceAdapter.getItem(position);
                if (provinceBean != null) {
                    this.mProTv.setText("" + provinceBean.getName());
                    this.mCityTv.setText("请选择");
                    this.mProvinceAdapter.updateSelectedPosition(position);
                    this.mProvinceAdapter.notifyDataSetChanged();
                    this.mCityAdapter = new CityAdapter(this.context, provinceBean.getCityList());
                    this.mHandler.sendMessage(Message.obtain(this.mHandler, 1, provinceBean.getCityList()));
                }
                break;
            case 1:
                CityBean cityBean = this.mCityAdapter.getItem(position);
                if (cityBean != null) {
                    this.mCityTv.setText("" + cityBean.getName());
                    this.mAreaTv.setText("请选择");
                    this.mCityAdapter.updateSelectedPosition(position);
                    this.mCityAdapter.notifyDataSetChanged();
                    this.mAreaAdapter = new AreaAdapter(this.context, cityBean.getCityList());
                    this.mHandler.sendMessage(Message.obtain(this.mHandler, 2, cityBean.getCityList()));
                }
                break;
            case 2:
                DistrictBean districtBean = this.mAreaAdapter.getItem(position);
                if (districtBean != null) {
                    this.callback(districtBean);
                }
        }

    }

    private void setProvinceListData() {
        this.provinceList = this.parseHelper.getProvinceBeanArrayList();
        if (this.provinceList != null && !this.provinceList.isEmpty()) {
            this.mProvinceAdapter = new ProvinceAdapter(this.context, this.provinceList);
            this.mCityListView.setAdapter(this.mProvinceAdapter);
        } else {
            ToastUtils.showLongToast(this.context, "解析本地城市数据失败！");
        }
    }

    public void init(Context context) {
        this.context = context;
        this.parseHelper = new CityParseHelper();
        if (this.parseHelper.getProvinceBeanArrayList().isEmpty()) {
            this.parseHelper.initData(context);
        }

    }

    private void updateIndicator() {
        this.popview.post(new Runnable() {
            public void run() {
                switch(JDCityPicker.this.tabIndex) {
                    case 0:
                        JDCityPicker.this.tabSelectedIndicatorAnimation(JDCityPicker.this.mProTv).start();
                        break;
                    case 1:
                        JDCityPicker.this.tabSelectedIndicatorAnimation(JDCityPicker.this.mCityTv).start();
                        break;
                    case 2:
                        JDCityPicker.this.tabSelectedIndicatorAnimation(JDCityPicker.this.mAreaTv).start();
                }

            }
        });
    }

    private AnimatorSet tabSelectedIndicatorAnimation(TextView tab) {
        ObjectAnimator xAnimator = ObjectAnimator.ofFloat(this.mSelectedLine, "X", new float[]{this.mSelectedLine.getX(), tab.getX()});
        final LayoutParams params = this.mSelectedLine.getLayoutParams();
        ValueAnimator widthAnimator = ValueAnimator.ofInt(new int[]{params.width, tab.getMeasuredWidth()});
        widthAnimator.addUpdateListener(new AnimatorUpdateListener() {
            public void onAnimationUpdate(ValueAnimator animation) {
                params.width = (Integer)animation.getAnimatedValue();
                JDCityPicker.this.mSelectedLine.setLayoutParams(params);
            }
        });
        AnimatorSet set = new AnimatorSet();
        set.setInterpolator(new FastOutSlowInInterpolator());
        set.playTogether(new Animator[]{xAnimator, widthAnimator});
        return set;
    }

    public void showCityPicker() {
        this.initJDCityPickerPop();
        if (!this.isShow()) {
            this.popwindow.showAtLocation(this.popview, 80, 0, 0);
        }

    }

    private void hidePop() {
        if (this.isShow()) {
            this.popwindow.dismiss();
        }

    }

    private boolean isShow() {
        return this.popwindow.isShowing();
    }

    @SuppressLint("WrongConstant")
    private void updateTabVisible() {
        this.mProTv.setVisibility(this.provinceList != null && !this.provinceList.isEmpty() ? 0 : 8);
        this.mCityTv.setVisibility(this.cityList != null && !this.cityList.isEmpty() ? 0 : 8);
        this.mAreaTv.setVisibility(this.areaList != null && !this.areaList.isEmpty() ? 0 : 8);
    }

    private void callback(DistrictBean districtBean) {
        ProvinceBean provinceBean = this.provinceList != null && !this.provinceList.isEmpty() && this.mProvinceAdapter != null && this.mProvinceAdapter.getSelectedPosition() != -1 ? (ProvinceBean)this.provinceList.get(this.mProvinceAdapter.getSelectedPosition()) : null;
        CityBean cityBean = this.cityList != null && !this.cityList.isEmpty() && this.mCityAdapter != null && this.mCityAdapter.getSelectedPosition() != -1 ? (CityBean)this.cityList.get(this.mCityAdapter.getSelectedPosition()) : null;
        this.mBaseListener.onSelected(provinceBean, cityBean, districtBean);
        this.hidePop();
    }

    @SuppressLint("WrongConstant")
    private void updateTabsStyle(int tabIndex) {
        switch(tabIndex) {
            case -1:
                this.mProTv.setTextColor(Color.parseColor(this.colorAlert));
                this.mProTv.setVisibility(0);
                this.mCityTv.setVisibility(8);
                this.mAreaTv.setVisibility(8);
                break;
            case 0:
                this.mProTv.setTextColor(Color.parseColor(this.colorAlert));
                this.mProTv.setVisibility(0);
                this.mCityTv.setVisibility(8);
                this.mAreaTv.setVisibility(8);
                break;
            case 1:
                this.mProTv.setTextColor(Color.parseColor(this.colorSelected));
                this.mCityTv.setTextColor(Color.parseColor(this.colorAlert));
                this.mProTv.setVisibility(0);
                this.mCityTv.setVisibility(0);
                this.mAreaTv.setVisibility(8);
                break;
            case 2:
                this.mProTv.setTextColor(Color.parseColor(this.colorSelected));
                this.mCityTv.setTextColor(Color.parseColor(this.colorSelected));
                this.mAreaTv.setTextColor(Color.parseColor(this.colorAlert));
                this.mProTv.setVisibility(0);
                this.mCityTv.setVisibility(0);
                this.mAreaTv.setVisibility(0);
        }

    }
}
